local skel = fk.CreateSkill {
  name = "rmt__xujin",
}

Fk:loadTranslationTable{
  ["rmt__xujin"] = "虚襟",
  [":rmt__xujin"] = "你造成或受到伤害后，你可以选择此回合未选择过一项，令数值相差不大于X（X为当前回合角色此回合造成的伤害值）的两名其他角色"..
  "交换对应值：1.手牌数；2.装备区牌数；3.判定区牌数。",

  ["#rmt__xujin-invoke"] = "虚襟：你可以令两名对应项差值不大于%arg的角色交换对应值",
  ["rmt__xujin_active"] = "虚襟",
  ["rmt__xujin_h"] = "交换手牌",
  ["rmt__xujin_e"] = "交换装备",
  ["rmt__xujin_j"] = "交换判定区",

  ["$rmt__xujin1"] = "虚心静志，去喜去恶。从室视庭，咫尺已具。",
  ["$rmt__xujin2"] = "赏者赏，刑者刑，因其所为，各以自成。",
}

---@param player ServerPlayer
local can_trigger = function (self, event, target, player, data)
  local room = player.room
  if target == player and player:hasSkill(skel.name) and #player:getTableMark("rmt__xujin-turn") < 3
  and #room:getOtherPlayers(player, false) > 1 then
    local turn_event = room.logic:getCurrentEvent():findParent(GameEvent.Turn)
    if turn_event == nil then return end
    local current = turn_event.data.who
    if current == nil then return end
    local n = 0
    room.logic:getActualDamageEvents(1, function (e)
      local damage = e.data
      if damage.from == current then
        n = n + damage.damage
      end
      return false
    end, Player.HistoryTurn)
    room:setPlayerMark(player, "rmt__xujin-tmp", n)
    local choices = {}
    for _, choice in ipairs({"h", "e", "j"}) do
      if not table.contains(player:getTableMark("rmt__xujin-turn"), choice) and
        table.find(room:getOtherPlayers(player, false), function (p)
          return table.find(room:getOtherPlayers(p, false), function (p2)
            return p2 ~= player and  math.abs(#p:getCardIds(choice) - #p2:getCardIds(choice)) <= n
          end) ~= nil
        end) ~= nil then
        table.insert(choices, "rmt__xujin_"..choice)
      end
    end
    if #choices > 0 then
      room:setPlayerMark(player, "rmt__xujin-tmp", {choices, n})
      return true
    end
  end
end

local on_cost = function (self, event, target, player, data)
  local room = player.room
  local success, dat = room:askToUseActiveSkill(player, {
    skill_name = "rmt__xujin_active", prompt = "#rmt__xujin-invoke:::"..player:getMark("rmt__xujin-tmp")[2],
  })
  room:setPlayerMark(player, "rmt__xujin-tmp", 0)
  if success and dat then
    room:sortByAction(dat.targets)
    event:setCostData(self, {tos = dat.targets, choice = dat.interaction })
    return true
  end
end

local on_use = function (self, event, target, player, data)
  local room = player.room
  local targets = event:getCostData(self).tos
  local choice = event:getCostData(self).choice:sub(-1, -1)
  room:addTableMark(player, "rmt__xujin-turn", choice)
  room:swapAllCards(player, targets, skel.name, choice)
end

skel:addEffect(fk.Damage, {
  anim_type = "control",
  can_trigger = can_trigger,
  on_cost = on_cost,
  on_use = on_use,
})

skel:addEffect(fk.Damaged, {
  anim_type = "control",
  can_trigger = can_trigger,
  on_cost = on_cost,
  on_use = on_use,
})

local skel2 = fk.CreateSkill {
  name = "rmt__xujin_active",
}

skel2:addEffect("active", {
  card_num = 0,
  target_num = 2,
  interaction = function (self, player)
    local choices = player:getTableMark("rmt__xujin-tmp")[1]
    return UI.ComboBox { choices = choices, all_choices = {"rmt__xujin_h", "rmt__xujin_e", "rmt__xujin_j"} }
  end,
  card_filter = Util.FalseFunc,
  target_filter = function (self, player, to_select, selected)
    if #selected < 2 and to_select ~= player then
      if #selected == 0 then
        return true
      else
        local target1 = selected[1]
        local area = self.interaction.data:sub(-1, -1)
        local n = player:getTableMark("rmt__xujin-tmp")[2]
        return math.abs(#target1:getCardIds(area) - #to_select:getCardIds(area)) <= n and
          not (#target1:getCardIds(area) == 0 and #to_select:getCardIds(area) == 0)
      end
    end
  end,
})

return {skel, skel2}
